package cn.stylefeng.guns.cp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.base.utils.DingUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/2/4 9:30
 **/
@Slf4j
@Component
public class Ao10Schedule implements CommandLineRunner {
    @Value("${path.url}")
    private String url;
    @Value("${path.sc}")
    private String scPath;

    @Override
    public void run(String... args) throws Exception {
//        new Thread(() -> {
//            List<String> nums = Arrays.asList("4", "9");
//            while (true) {
//                String mm = DateUtil.format(new Date(), "mm").substring(1);
//                log.info(mm);
//                if (nums.contains(mm)) {
//                    Integer ss = Integer.valueOf(DateUtil.format(new Date(), "ss"));
//                    try {
//                        execute();
//                    } catch (Exception e) {
//                        log.error(e.getMessage(), e);
//                    }
//                    ThreadUtil.sleep(60 - ss, TimeUnit.SECONDS);
//                } else {
//                    ThreadUtil.sleep(60000);
//                }
//            }
//        }).start();
    }

    public void execute() {
        JSONObject result = getResult();
        JSONObject jsonObject = result.getJSONObject("result").getJSONArray("data").getJSONObject(0);
        String[] codes = jsonObject.getString("preDrawCode").split(",");
        String issue = jsonObject.getString("preDrawIssue");

        Sc10 sc10 = new Sc10(issue, codes);
        List<File> files = getFiles();

        String res = checkCode(sc10, files);

        DingUtil.send(res, "5");
    }

    public List<File> getFiles() {
        List<File> fileList = Lists.newArrayList();
        File file = new File(scPath);
        File[] files = file.listFiles();
        for (File f : files) {
            fileList.add(f);
        }
        return fileList;
    }

    public String checkCode(Sc10 sc10, List<File> files) {
        String lxCode = sc10.getLxCode(4);
        List<String> list = Lists.newArrayList();
        String date = DateUtil.format(new Date(), "dd HH:mm");
        list.add(sc10.getIssue() + "_" + date);
        list.add(sc10.getCodes());
        for (File f : files) {
            List<String> codes = FileUtil.readLines(f, Charset.defaultCharset());
            if (codes.contains(lxCode)) {
                list.add(StrUtil.format("{}-true", f.getName()));
            } else {
                list.add(StrUtil.format("{}-false", f.getName()));
            }
        }
        return CollUtil.join(list, "\n");
    }

    public JSONObject getResult() {
        String date = DateUtil.format(new Date(), "yyyy-MM-dd");
        String reqUrl = StrUtil.format("{}/cp/ao?date={}&lotCode={}", url, date, "10012");
        String res = HttpUtil.createGet(reqUrl).execute().body();
        return JSONObject.parseObject(res);
    }

    public static void main(String[] args) {
        String reqUrl = StrUtil.format("{}/cp/ao?date=&lotCode={}", "http://124.222.75.100:8089", "10012");
        String res = HttpUtil.createGet(reqUrl).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(res);

        System.out.println(jsonObject);
        JSONArray datas = jsonObject.getJSONObject("result").getJSONArray("data");
        datas.forEach(data -> {
            JSONObject jsonObject1 = (JSONObject) data;
            String preDrawCode = jsonObject1.getString("preDrawCode");
            String preDrawIssue = jsonObject1.getString("preDrawIssue");

            String[] codes = preDrawCode.split(",");
            String str = StrUtil.format("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}",
                    preDrawIssue,
                    codes[0],
                    codes[1],
                    codes[2],
                    codes[3],
                    codes[4],
                    codes[5],
                    codes[6],
                    codes[7],
                    codes[8]
            );
            System.out.println(str);
        });
    }
}
